[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
Turbo Pascal Statements
Case - Conditional Statement
The Case statement consists of an expression (The Selector), followed
by a list of statements. Each statement in the list is prefixed with
at least one constant (Case Constant) or with the reserved word, Else.
The Selector must be of byte- or word-sized ordinal type. All Case
Constants must be unique and of a ordinal type that is compatible with
the Selector type.
A statement is executed if it is prefixed by a Case Constant that is
equal to the value of the Selector or a Case Range that contains the
value of the Selector. If no such constant or range exists, and an
else part is present, the statement that following else is executed.
If there is no else part present, no statements are executed.
Case statement
+------------+ +------+
Case --.| expression |-. of .| case |------------------------------. End
+------------+ . +------+ | | | | |
| | | | | |
| | | +-----------+ | | |
+---- ; .--+ +.| else part |.+ +. ; .+
+-----------+
Case
+----------------------+
| |
+----------+ | +----------+ .
---.| constant |---. .. --.| constant |-----. : statement
. +----------+ +----------+ |
| (Case Range) |
| |
+------------ , -----------------------+
(Constant List)
Else part
+-----------+
Else ---.| statement |----.
+-----------+
EXAMPLE: Var
OrdVal : Integer;
Begin
ReadLn(OrdVal);
Case OrdVal of
1..3 : Begin
Write('OrdVal is 1 or');
WriteLn(' OrdVal is 3);
End;
5, 7 : WriteLn('OrdVal is 5 or 7');
10 : WriteLn('OrdVal is 10 ;
Else
WriteLn('OrdVal is',OrdVal);
End; (* end case *)
End.
See Also:
Ordinal
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson